package com.ruoyi.web.controller.wechat;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wechat.domain.WxAccount;
import com.ruoyi.wechat.service.WxAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 微信网页授权相关
 *
 * @author yan
 */
@RestController
@RequestMapping("/wechat/wxAccount")
@Api(tags = {"微信网页授权-账号"})
@RequiredArgsConstructor
public class WxAccountController {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    private final WxMpService wxMpService;

    @Autowired
    private WxAccountService wxAccountService;

    /**
     * 添加公众号
     *
     * @param wxAccount
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wechat:account:add')")
    @ApiOperation(value = "添加公众号")
    @PostMapping("/add")
    public AjaxResult addWxAccount(@RequestBody WxAccount wxAccount) {

        return wxAccountService.save(wxAccount) ? AjaxResult.success("添加公众号成功") : AjaxResult.error("添加公众号失败");
    }

    /**
     * 列表
     */
    @PreAuthorize("@ss.hasPermi('wechat:account:query')")
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public AjaxResult list(Page page, WxAccount wxAccount) {
        return AjaxResult.success("公众号列表", wxAccountService.selectWxAccountList(page, wxAccount));
    }

    /**
     * 信息
     */
    @PreAuthorize("@ss.hasPermi('wechat:account:query')")
    @GetMapping("/info/{appid}")
    @ApiOperation(value = "详情")
    public AjaxResult info(@PathVariable String appid) {
        WxAccount wxAccount = wxAccountService.getById(appid);

        return AjaxResult.success("wxAccount", wxAccount);
    }


    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('wechat:account:remove')")
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    public AjaxResult delete(String[] appids) {
        return AjaxResult.success(wxAccountService.removeByIds(Arrays.asList(appids)));
    }
}
